package com.miyake.client.tools;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.client.datamanage.Text;

public class EditableText extends Composite {
	public interface EditableTextHandler {
		void onTextUpdate(Text text);
	}
	private List<EditableTextHandler> handlers = new ArrayList<EditableTextHandler>();
	public void addEditableTextHandler(EditableTextHandler handler) {
		handlers.add(handler);
	}
	protected void fireTextUpdateEvent(Text text) {
		for (EditableTextHandler handler : handlers) {
			handler.onTextUpdate(text);
		}
	}
	private HTML label;
	private TextArea textBox;
	private VerticalPanel panel;
	private Button button;
	private boolean buttonEnabled = true;
	private CheckBox bold;
	private HorizontalPanel toolBar;
	private ListBox alignment;
	
	public EditableText(Text text) {
		initialize();
		setText(text);
	}
	
	public EditableText() {
		initialize();
	}

	private void initialize() {
		panel = new VerticalPanel();
		initWidget(panel);
		label = new HTML();
		label.setWidth("100%");
		label.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				setEdit(true);
			}
		});
		textBox = new TextArea();
		textBox.setWidth("100%");
		textBox.setHeight("150px");
		textBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					//finishEdit();
				}
				else if(event.getCharCode() == KeyCodes.KEY_ESCAPE) {
					finishEdit();
				}
			}
		});
		
		toolBar = new HorizontalPanel();
		button = new SmallButton("Finish");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				finishEdit();
			}
		});
		button.setVisible(buttonEnabled);
		
		bold = new CheckBox("Bold");
		bold.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				updateLabel();
			}
		});
		
		alignment = new ListBox();
		alignment.addItem("Left");
		alignment.addItem("Center");
		alignment.addItem("Right");
		alignment.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				updateLabel();
			}
		});
		toolBar.add(button);
		toolBar.add(bold);
		toolBar.add(alignment);
		panel.add(toolBar);
		panel.add(textBox);
		panel.add(label);
		setEdit(false);
	}
	
	private void updateLabel() {
		
		String html = new FormatLabel() {
			@Override
			public boolean isBold() {
				return bold.getValue();
			}

			@Override
			public int getHorizontalAlign() {
				return alignment.getSelectedIndex();
			}
			
		}.getHTML(textBox.getText());
		
		label.setHTML(html);
	}
	
	public void setText(Text text) {
		bold.setValue(false);
		if (text.getBold() != null && text.getBold().equals(true)) {
			bold.setValue(true);
		}

		if (text.getAlign() != null) {
			alignment.setSelectedIndex(text.getAlign());
		}
		else {
			alignment.setSelectedIndex(0);
		}
		textBox.setText(text.getText());
		updateLabel();
	}
	
	public String getText() {
		return textBox.getText();
	}
	
	public void setEdit(boolean edit) {
		if (edit) {
			textBox.setText(label.getText());
			toolBar.setVisible(buttonEnabled);
			textBox.setVisible(true);
			label.setVisible(false);
		}
		else {
			toolBar.setVisible(false);
//			label.setText(textBox.getText());
			updateLabel();
			textBox.setVisible(false);
			label.setVisible(true);
		}
	}
	private void finishEdit() {
		setEdit(false);
		Text text = new Text();
		text.setText(textBox.getText());
		text.setBold(bold.getValue());
		text.setAlign(alignment.getSelectedIndex());
		fireTextUpdateEvent(text);
	}
	public boolean isButtonEnabled() {
		return buttonEnabled;
	}
	public void setButtonEnabled(boolean buttonEnabled) {
		this.buttonEnabled = buttonEnabled;
	}
}
